package com.ucampus.service;

import com.ucampus.dao.ArticleDao;
import com.ucampus.entity.Article;
import com.ucampus.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;

    public ResponseData getAll() {
        ResponseData responseData = new ResponseData();
        List<Article> articles = articleDao.getAll();
        if (!articles.isEmpty()) {
            responseData.setMessage("查询成功");
            responseData.setData(articles);
        } else {
            responseData.setMessage("查询失败，请重试");
        }
        return responseData;
    }

    public ResponseData delete(List<Integer> list) {
        ResponseData responseData = new ResponseData();
        int n = articleDao.delete(list);
        if (n > 0) {
            responseData.setMessage("删除成功"+n+"条数据已删除，请刷新页面获取最新数据");
        } else {
            responseData.setMessage("删除失败，请重试");
        }
        return responseData;
    }

    public ResponseData addArticle(Article article) {
        ResponseData responseData = new ResponseData();
        int isUpLoad = articleDao.insert(article);
        if (isUpLoad > 0) {
            responseData.setMessage("发布成功");
        } else {
            responseData.setMessage("发布失败，请重试");
        }
        return responseData;
    }

    public ResponseData search(String keyword) {
        ResponseData responseData = new ResponseData();
        List<Article> articles = articleDao.searchByKeyword(keyword);
        if (!articles.isEmpty()) {
            responseData.setMessage("查询成功");
            responseData.setData(articles);
        } else {
            responseData.setMessage("未找到相关内容");
        }
        return responseData;
    }

    public ResponseData getArticlesByUserId(long userId) {
        ResponseData responseData = new ResponseData();
        List<Article> articles = articleDao.getArticlesByUserId(userId);
        if (!articles.isEmpty()) {
            responseData.setMessage("查询成功");
            responseData.setData(articles);
        } else {
            responseData.setMessage("该用户没有发布任何帖子");
        }
        return responseData;
    }
}
